import jwt
import time
from eim import secret_key
from eim import tokenScheme

class tokenTool:
    """
    认证token
    """
    def __init__(self):
        pass
    
    def createToken(self, usercode, name, uid, exprietime: float):
        """
        根据用户名及id获取token
        :param uno: 用户编码
        :param uname: 用户名
        :param uid: 用户id
        :param exprietime: 过期时长分钟
        :return: token
        """
        try:
            # 定义token结构
            time_start = time.time()
            tokenstruct: dict = {
                "exp": time_start + (exprietime * 60),
                "iat": time_start,
                "iss": "com.jasonzhang1224.163",
                "data": {
                    "usercode": usercode,
                    "name": name,
                    "id": uid
                }
            }
            token = jwt.encode(tokenstruct, key=secret_key)
            return f"{tokenScheme} {token}"
        except Exception as es:
            raise(es)